package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum MonitorTypeStruct implements EnumTrans<CompleteEntity.Parameter.MonitorTypeStruct, MonitorTypeStruct> {
    /**
     * 默认属性
     * RcvCard
     * FunctionCard
     * SmartModule
     * Player
     * Sender
     * 加密上报
     */
    MonitorTypeStructDefault(CompleteEntity.Parameter.MonitorTypeStruct.MonitorTypeStructDefault),
    RcvCard(CompleteEntity.Parameter.MonitorTypeStruct.RcvCard),
    FunctionCard(CompleteEntity.Parameter.MonitorTypeStruct.FunctionCard),
    SmartModule(CompleteEntity.Parameter.MonitorTypeStruct.SmartModule),
    Sender(CompleteEntity.Parameter.MonitorTypeStruct.Sender),
    Authorization(CompleteEntity.Parameter.MonitorTypeStruct.Authorization),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.Parameter.MonitorTypeStruct monitorTypeStruct;
    private static final long serialVersionUID = 987654321987654321L;

    MonitorTypeStruct(CompleteEntity.Parameter.MonitorTypeStruct monitorTypeStruct) {
        this.id = monitorTypeStruct.getNumber();
        this.name = monitorTypeStruct.name();
        this.monitorTypeStruct = monitorTypeStruct;
    }

    /**
     * @param monitorTypeStruct
     * @return 对应的类型
     */
    public static MonitorTypeStruct getMonitorTypeStruct(CompleteEntity.Parameter.MonitorTypeStruct monitorTypeStruct) {
        Objects.requireNonNull(monitorTypeStruct, "monitorTypeStruct can not be null.");
        return Arrays.stream(MonitorTypeStruct.values())
                .filter(e -> e.name.equals(monitorTypeStruct.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.Parameter.MonitorTypeStruct toProto() {
        return this.monitorTypeStruct;
    }

    @Override
    public MonitorTypeStruct formProto(CompleteEntity.Parameter.MonitorTypeStruct monitorTypeStruct) {
        return getMonitorTypeStruct(monitorTypeStruct);
    }
}

